<?php

namespace App\Library\Message\Channels;

use App\Components\SystemNotice\Models\SystemNotice;
use Illuminate\Notifications\Notification;

class SystemNoticeChannel
{
    public function send($notifiable, Notification $notification)
    {
        if (! $to = $notifiable->routeNotificationFor('system_notice', $notification)) {
            return;
        }

        /** @var \EasyWeChat\Kernel\Messages\Message|string $message */
        $message = $notification->toSystemNotice($notifiable);

        if (is_null($message)) {
            return;
        }

        $title = preg_replace('/\n/', '<br>', $message['title']);

        SystemNotice::create([
            'title' => $title,
            'bg' => $message['bg'],
            'link' => $message['link'],
            'to_user_id' => $to,
        ]);
    }
}
